package com.yq.dataStructures.tree;

/**
 * @ClassName ArrBinaryTreeDemo
 * @Description 顺序存储二叉树
 * @Author yaoqiang
 * @Date 2021/11/15 11:54
 * @Version 1.0
 */
public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr= {1,2,3,4,5,6,7};
        ArrBinaryTree tree=new ArrBinaryTree(arr);
        tree.preOrder();//1245367
    }
}

class  ArrBinaryTree{
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public void preOrder() {
        preOrder(0);
    }

    /**
     * 顺序存储的前序
     * @param i
     */
    private void preOrder(int i) {
        System.out.println(arr[i]);
        if ((i*2+1)<arr.length){
            preOrder(i*2+1);
        }
        if ((i*2+2)<arr.length){
            preOrder(i*2+2);
        }
    }
}